package com.design.springboot.booksharing.controller;

import com.design.springboot.booksharing.annotation.AllowNoToken;
import com.design.springboot.booksharing.bean.ActiveUser;
import com.design.springboot.booksharing.bean.ApiResult;
import com.design.springboot.booksharing.bean.User;
import com.design.springboot.booksharing.component.ApiResultComponent;
import com.design.springboot.booksharing.constant.SystemConstant;
import com.design.springboot.booksharing.enums.ApiResultCodeEnum;
import com.design.springboot.booksharing.service.UserService;
import com.design.springboot.booksharing.utils.endecrypt.Md5Utils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * 登录接口服务
 *
 * @author admin
 */
@Slf4j
@AllowNoToken
@RestController
@RequestMapping("/auth")
public class LoginController {
    @Resource
    ApiResultComponent apiResultComponent;

    @Resource
    private UserService userService;

    @Value("${system.manager.account}")
    private String systemManagerAccount;

    /**
     * 登录
     *
     * @param username 登录账号(学号)
     * @param password 密码
     */
    @PostMapping("/login")
    public ApiResult<ActiveUser> login(HttpSession session, String username, String password) {
        try {
            if (StringUtils.isEmpty(username)) {
                return apiResultComponent.error(ApiResultCodeEnum.USERNAME_PATTERN_ERROR);
            }

            User user = userService.getUserByAccount(username);
            if (user != null) {
                password = Md5Utils.hash(password);

                if (user.getPassword().equals(password)) {
                    ActiveUser activeUser = new ActiveUser();
                    activeUser.setUser(user);
                    activeUser.setAccount(user.getAccount());
                    activeUser.setSessionId(session.getId());
                    activeUser.setUserId(user.getId());

                    activeUser.setManager(systemManagerAccount.equals(username));
                    session.setAttribute(SystemConstant.ACTIVE_USER, activeUser);
                    return apiResultComponent.success(activeUser);
                } else {
                    return apiResultComponent.error(ApiResultCodeEnum.PASSWORD_ERROR);
                }
            }
            return apiResultComponent.error(ApiResultCodeEnum.USER_NULL);
        } catch (Exception ex) {
            ex.printStackTrace();
            log.error(ex.getMessage(), ex);
            return apiResultComponent.error(ApiResultCodeEnum.ERROR);
        }
    }

    /**
     * 退出登录
     */
    @PostMapping("/logout")
    public ApiResult<ActiveUser> logout(HttpSession session) {
        try {
            System.out.println("session = " + session.getId());
            session.invalidate();
            return apiResultComponent.success();
        } catch (Exception ex) {
            ex.printStackTrace();
            log.error(ex.getMessage(), ex);
            return apiResultComponent.error(ApiResultCodeEnum.ERROR);
        }
    }
}
